﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using ERP.Source.Interface;
using ERP.Source.DBBridge;
using ERP.Source.Core;

namespace ERP.Source.Interface
{
    /// <summary>
    /// Interaction logic for MainWindows.xaml
    /// Ham chinh xu ly
    /// Cac thuoc tinh :
    ///     - isConnected : Luu gia tri da ket noi duoc co so du lieu hay chua
    ///     - user : Luu gia tri thong tin nguoi da dang nhap vao he thong, de hien thi ra man hinh
    ///     - login : De tao ra form login, muc dich de nguoi dung dang nhap
    /// Cac phuong thuc :
    ///     - ConnectDatabase : Thuc hien ket noi du lieu, va tra ve ket qua da dang nhap thanh cong hay chua vao bien isConnected
    ///     - Loading : Thuc hien chay chuong trinh, phuong thuc duoc thuc hien khi khoi dong chuong trinh
    ///     - Login : Khoi dong form dang nhap LoginUI, thuc hien login sau khi nguoi dung nhap dung username va password
    ///     - SetValidFunction : Thuc hien dong mo chuc nang khi dang nhap thanh cong
    ///     - ClearContentGrid : Thuc hien xoa noi dung cua Grid trung tam, noi dung de hien thi cac UI
    /// </summary>

    public partial class MainWindows : RibbonWindow
    {
        private bool isConnected;

        private User user; //bien dong vai tro luu giu thong tin tam thoi

        private LoginUI login; //bien lien ket den class LoginUI
        private ManageGroupOfMaterialUI manageGroupOfMaterial; //bien lien ket den class ManageGroupOfMaterialUI

        public User User
        {
            set
            {
                user = value;
            }
            get
            {
                return user;
            }
        }

        public bool IsConnected
        {
            set
            {
                isConnected = value;
            }
            get
            {
                return isConnected;
            }
        }

        public MainWindows()
        {
            InitializeComponent();
            // Insert code required on object creation below this point.
        }

        public void ConnectDatabase()
        {
            //thuc hien ket noi co so du lieu
            isConnected = false;

            Database.DBName = "db";
            isConnected = Database.Connect();
        }

        public void Loading() 
        {
            ConnectDatabase();
            
            //hien thi form main
            //this.IsEnabled = false;
            this.Show();

            //thuc hien login
            if (isConnected)
            {
                //Login();
            }
        }

        public void Login()
        {
            //khoi dong form dang nhap
            login = new LoginUI();
            login.ShowDialog();

            //luu thong tin tai khoan dang nhap vao bien user
            user = login.User;

            if (login.Status == true)
            {
                //thiet lap duoc phep va ko duoc phep
                SetValidFunction(login.Level);
            }
        }

        public void SetValidFunction(short level = 1)
        {
            //thuc hien set nhung quyen phu hop cho nguoi su dung
            //co 4 muc tuy thuoc vao loai nhan vien (level) tu 1 den 4
            //min = 1, max = 4

            this.IsEnabled = true;

            switch (level)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                default:
                    break;
            }
        }

        private void ClearContentGrid()
        {
            if (manageGroupOfMaterial != null)
            {
                grdRoot.Children.Remove(manageGroupOfMaterial);
            }
        }

        private void frmMain_Loaded(object sender, RoutedEventArgs e)
        {
            Loading();
        }

        private void cmdLogin_Click(object sender, RoutedEventArgs e)
        {
            if (!isConnected)
            {
                ConnectDatabase();
            }
            if (isConnected)
            {
                Login();
            }
        }

        private void frmMain_Closed(object sender, EventArgs e)
        {
            Database.Disconnect();
        }

        private void cmdGroupOfMaterial_Click(object sender, RoutedEventArgs e)
        {
            ClearContentGrid();
            if (manageGroupOfMaterial == null)
            {
                manageGroupOfMaterial = new ManageGroupOfMaterialUI();
            }
            grdRoot.Children.Add(manageGroupOfMaterial);
            Grid.SetRow(manageGroupOfMaterial, 1);
        }
    }
}
